package com.web.magic.tom.control;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.web.magic.catcher.TopGeter;
import com.web.magic.catcher.model.SearchEnum;
import com.web.magic.tom.constant.Contants;
import com.zsw.qr.GetImg;

@Controller
@RequestMapping("")
public class Control {
	
	@Resource
	private TopGeter test;
	
	@RequestMapping(value = "/test")
	@ResponseBody
	public Map<String, Object> payInvoke(@RequestParam(required = true)String topic, String platform){
		Map<String, Object> map = new HashMap<>();
		map.put("items", test.getCurrentTop10(SearchEnum.valueOf(topic.toUpperCase())));
		return map;
	}
	@RequestMapping(value = "/img")
	public void generateImg(@RequestParam(required = true)String name, HttpServletResponse response){
		BufferedImage image = GetImg.getImg(name, Contants.url);
	    response.setContentType("image/jpg");
	    OutputStream os;
		try {
			os = response.getOutputStream();
			ImageIO.write(image, "jpg", os);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(value = "/v")
	public String redirectImg(){
		return "redirect:/v/index.html";
	}
	
}
